<!--
Copyright 2012 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<script type="text/javascript" src="nacltest.js"></script>

<div id="scratch_space"></div>

<script type="text/javascript">

var tester = new Tester();

tester.addAsyncTest('trusted_crash_during_startup', function(status) {
  var embed = document.createElement('embed');
  embed.width = 0;
  embed.height = 0;
  // We trigger a crash in trusted code via the NACL_CRASH_TEST env var.
  // Since the crash occurs during startup, untrusted code does not run,
  // so the choice of NaCl executable is arbitrary.
  embed.src = 'crash_test.nmf';
  embed.type = 'application/x-nacl';
  embed.name = 'foo';

  // Webkit Bug Workaround
  // THIS SHOULD BE REMOVED WHEN Webkit IS FIXED
  // http://code.google.com/p/nativeclient/issues/detail?id=2428
  // http://code.google.com/p/chromium/issues/detail?id=103588
  ForcePluginLoadOnTimeout(embed, tester, 15000);

  var div = document.createElement('div');
  div.appendChild(embed);

  div.addEventListener('load', status.wrap(function(event) {
    status.fail('We expected this process to crash during startup');
  }), true);

  div.addEventListener('error', status.wrap(function(event) {
    var error = embed.lastError;
    status.log('Received error: ' + error);
    // This error occurs only because NACL_CRASH_TEST is set.  We would
    // not normally expect to get this error in the browser.
    var expected_error_prefix = 'NaCl module load failed: ServiceRuntime: ';
    var suffix1 = 'command channel creation failed';
    var suffix2 = 'failed to start';
    status.assert(error == expected_error_prefix + suffix1 ||
                  error == expected_error_prefix + suffix2);
    status.pass();
  }), true);

  document.getElementById('scratch_space').appendChild(div);
});

tester.run();

</script>
